package com.twu.biblioteca.commandexecutor.subexecutors;

import com.twu.biblioteca.commandexecutor.BibliotecaCommandExecutor_SubModules;
import com.twu.biblioteca.commandexecutor.ProgramExitRequestedException;
import com.twu.biblioteca.commandexecutor.WrongCommandFormatException;
import com.twu.biblioteca.librarycollection.LibraryCollection;
import com.twu.biblioteca.memberdata.Member;
import com.twu.biblioteca.memberdata.MemberData;
import com.twu.biblioteca.memberdata.WrongUsernameOrPasswordException;
import com.twu.biblioteca.memberdata.UserDetails;

public class LoginSubExecutor implements BibliotecaCommandExecutor_SubModules.SubExecutor
    {
        int menuPos = -1;
        public static final String wrongCommandFormatMessage = "Please provide both username and password.";;
        public static final String loginSuccessful = "Login successful, welcome ";
        public static final String loginUnsuccessful = "Login unsuccessful, wrong username or password.";

        String username = null;
        String password = null;
        static final String enterUsernameMessage = "User ID: ";
        static final String enterPasswordMessage = "Password: ";
        boolean isDone = false;
        boolean commandCalled = false;


        public LoginSubExecutor(int menuPos)
        {
            this.menuPos = menuPos;
        }

        public String getMenuOptionText()
        {
            return menuPos + "";
        }

        public String getMenuOptionDescription()
        {
            return "Login";
        }

        public boolean canExecute(String command)
        {
            if(command.startsWith(menuPos+""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public String execute(LibraryCollection libraryColl, MemberData memberData, UserDetails userDetails, String command) throws ProgramExitRequestedException
        {
            if(commandCalled == false)
            {
                commandCalled = true;
                return enterUsernameMessage;
            }
            else if(username == null)
            {
                doRetrieveUsername(command);
                return enterPasswordMessage;
            }
            else
            {
                doRetrievePassword(command);
                return doProcessLogin_CatchBadLoginException(memberData, userDetails);
            }
        }

        private void doRetrieveUsername(String command) {
            username = command.trim();
        }

        private void doRetrievePassword(String command) {
            password = command.trim();
        }

        public String doProcessLogin_CatchBadLoginException(MemberData memberData, UserDetails userDetails)
        {
            String msg = "";
            try
            {
                msg = doAttemptLogin(memberData, userDetails);
            }
            catch(WrongUsernameOrPasswordException wuope)
            {
                msg = loginUnsuccessful + "\n";
            }
            isDone = true;
            return msg;
        }

        public String doAttemptLogin(MemberData memberData, UserDetails userDetails) throws WrongUsernameOrPasswordException {
            Member member = memberData.getMemberByUsernamePassword(username, password);
            userDetails.setLoggedInAs(member);
            isDone = true;
            return loginSuccessful + member.getId() + ".\n";
        }


        @Override
        public boolean isDone() {
            return isDone;
        }

        @Override
        public void reset() {
            username = password = null;
            commandCalled = false;
            isDone = false;
        }
    }